﻿using System.Collections.Generic;
using Beisen.Data;
using fKanban.Model;
using fKanban.Utility.ObjectExtend;

namespace fKanban.Provider
{
    public class UserStoryProvider
    {
        public int Add(UserStoryEntity e)
        {
            //System.Data.SqlClient.SqlConnectionStringBuilder sb = new System.Data.SqlClient.SqlConnectionStringBuilder();
            //sb.DataSource = "10.3.136.211";
            //sb.UserID = "Devuser";
            //sb.Password = "24E7E0C8-16D4-4C6C-8E7E-3741A97E4486";
            //sb.InitialCatalog = "fkanban";
            //var conn = sb.ConnectionString;
            Database db = Database.GetDatabase("fkanban");
            var identity = SafeProcedure.ExecuteScalar(db, "dbo.jUserStoryAdd", delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@Role", e.Role);
                parameters.AddWithValue("@Ability", e.Ability);
                parameters.AddWithValue("@Benefit", e.Benefit);
                parameters.AddWithValue("@Importance", e.Importance);
                parameters.AddWithValue("@Estimate", e.Estimate);

                parameters.AddWithValue("@Name", e.Name);
                parameters.AddWithValue("@Owner", e.Owner);
                parameters.AddWithValue("@Status", e.Status);
                parameters.AddWithValue("@Master", e.Master);
                parameters.AddWithValue("@Notes", e.Notes);
            });
            return identity.ToInt();
        }
        public int Update(UserStoryEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            return SafeProcedure.ExecuteNonQuery(db, "dbo.jUserStoryUpdateByPara",
            delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@ID", e.Id);
                parameters.AddWithValue("@Role", e.Role);
                parameters.AddWithValue("@Ability", e.Ability);
                parameters.AddWithValue("@Benefit", e.Benefit);
                parameters.AddWithValue("@Importance", e.Importance);
                parameters.AddWithValue("@Estimate", e.Estimate);

                parameters.AddWithValue("@Name", e.Name);
                parameters.AddWithValue("@Owner", e.Owner);
                parameters.AddWithValue("@Master", e.Master);
                parameters.AddWithValue("@Status", e.Status);
                parameters.AddWithValue("@Notes", e.Notes);
            });
        }
        public int Remove(UserStoryEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            return SafeProcedure.ExecuteNonQuery(db, "dbo.jUserStoryRemoveByPara",
            delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@ID", e.Id);
            });
        }

        //todo:按DASHBOARDID加载需求池
        public List<UserStoryEntity> LoadData(int dashboardId)
        {
            return LoadData();
        }
        public List<UserStoryEntity> LoadData()
        {
            Database db = Database.GetDatabase("fkanban");
            return SafeProcedure.ExecuteAndGetInstanceList<UserStoryEntity>(db, "dbo.jUserStoryGetAll",
            delegate(IParameterSet parameters)
            {
                //nothing here
            },
            delegate(IRecord record, UserStoryEntity objectInstance)
            {
                objectInstance.Ability = record.GetOrDefault<string>("Ability", string.Empty);
                objectInstance.Benefit = record.GetOrDefault<string>("Benefit", string.Empty);
                objectInstance.Estimate = record.GetOrDefault<int>("Estimate", 0);
                objectInstance.Id = record.GetOrDefault<int>("Id", 0);
                objectInstance.Importance = record.GetOrDefault<int>("Importance", 0);

                objectInstance.Name = record.GetOrDefault<string>("Name", string.Empty);
                objectInstance.Notes = record.GetOrDefault<string>("Notes", string.Empty);
                objectInstance.Owner = record.GetOrDefault<string>("Owner", string.Empty);
                objectInstance.Master = record.GetOrDefault<string>("Master", string.Empty);
                objectInstance.MasterId = record.GetOrDefault<int>("MasterId", 0);
                objectInstance.Role = record.GetOrDefault<string>("Role", string.Empty);
                objectInstance.Status = record.GetOrDefault<int>("Status", 0);
            });
        }
        public UserStoryEntity LoadDataById(UserStoryEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            return SafeProcedure.ExecuteAndGetInstance<UserStoryEntity>(db, "dbo.jUserStoryGetListByPara",
            delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@ID", e.Id);
                parameters.AddWithValue("@Role", e.Role);
                parameters.AddWithValue("@Ability", e.Ability);
                parameters.AddWithValue("@Benefit", e.Benefit);
                parameters.AddWithValue("@Importance", e.Importance);
                parameters.AddWithValue("@Estimate", e.Estimate);

                parameters.AddWithValue("@Name", e.Name);
                parameters.AddWithValue("@Owner", e.Owner);
                parameters.AddWithValue("@Master", e.Master);
                parameters.AddWithValue("@Status", e.Status);
                parameters.AddWithValue("@Notes", e.Notes);
            },
            delegate(IRecord record, UserStoryEntity objectInstance)
            {
                objectInstance.Ability = record.GetOrDefault<string>("Ability", string.Empty);
                objectInstance.Benefit = record.GetOrDefault<string>("Benefit", string.Empty);
                objectInstance.Estimate = record.GetOrDefault<int>("Estimate", 0);
                objectInstance.Id = record.GetOrDefault<int>("Id", 0);
                objectInstance.Importance = record.GetOrDefault<int>("Importance", 0);

                objectInstance.Name = record.GetOrDefault<string>("Name", string.Empty);
                objectInstance.Notes = record.GetOrDefault<string>("Notes", string.Empty);
                objectInstance.Owner = record.GetOrDefault<string>("Owner", string.Empty);
                objectInstance.Master = record.GetOrDefault<string>("Master", string.Empty);
                objectInstance.MasterId = record.GetOrDefault<int>("MasterId", 0);
                objectInstance.Role = record.GetOrDefault<string>("Role", string.Empty);
                objectInstance.Status = record.GetOrDefault<int>("Status", 0);
            });
        }
    }
}